import dotenv from 'dotenv';
import path from 'path';

// 加载环境变量
dotenv.config();

interface Config {
  env: string;
  port: number;
  appName: string;
  database: {
    host: string;
    port: number;
    name: string;
    user: string;
    password: string;
  };
  redis: {
    host: string;
    port: number;
    password: string;
    db: number;
  };
  jwt: {
    secret: string;
    expiresIn: string;
    refreshExpiresIn: string;
  };
  bcrypt: {
    rounds: number;
  };
  rateLimit: {
    duration: number;
    max: number;
  };
  superAdmin: {
    email: string;
    password: string;
  };
  log: {
    level: string;
    filePath: string;
  };
  upload: {
    maxSize: number;
    path: string;
  };
  app: {
    domain: string;
  };
}

const config: Config = {
  env: process.env.NODE_ENV || 'development',
  port: parseInt(process.env.PORT || '4321', 10),
  appName: process.env.APP_NAME || '多租户SaaS平台',

  database: {
    host: process.env.DB_HOST || 'localhost',
    port: parseInt(process.env.DB_PORT || '3306', 10),
    name: process.env.DB_NAME || 'saas_platform',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || '',
  },

  redis: {
    host: process.env.REDIS_HOST || 'localhost',
    port: parseInt(process.env.REDIS_PORT || '6379', 10),
    password: process.env.REDIS_PASSWORD || '',
    db: parseInt(process.env.REDIS_DB || '0', 10),
  },

  jwt: {
    secret: process.env.JWT_SECRET || 'your-super-secret-jwt-key',
    expiresIn: process.env.JWT_EXPIRES_IN || '7d',
    refreshExpiresIn: process.env.JWT_REFRESH_EXPIRES_IN || '30d',
  },

  bcrypt: {
    rounds: parseInt(process.env.BCRYPT_ROUNDS || '10', 10),
  },

  rateLimit: {
    duration: parseInt(process.env.RATE_LIMIT_DURATION || '60000', 10),
    max: parseInt(process.env.RATE_LIMIT_MAX || '100', 10),
  },

  superAdmin: {
    email: process.env.SUPER_ADMIN_EMAIL || 'admin@saas.com',
    password: process.env.SUPER_ADMIN_PASSWORD || 'Admin@123456',
  },

  log: {
    level: process.env.LOG_LEVEL || 'info',
    filePath: process.env.LOG_FILE_PATH || path.join(process.cwd(), 'logs'),
  },

  upload: {
    maxSize: parseInt(process.env.UPLOAD_MAX_SIZE || '5242880', 10),
    path: process.env.UPLOAD_PATH || path.join(process.cwd(), 'uploads'),
  },

  app: {
    domain: process.env.APP_DOMAIN || 'saas.com',
  },
};

export default config;

